package com.topera.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.topera.eduservice.entity.EduCourse;
import com.topera.eduservice.entity.EduCourseDescription;
import com.topera.eduservice.entity.frontvo.CourseFrontVo;
import com.topera.eduservice.entity.frontvo.CourseWebVo;
import com.topera.eduservice.entity.vo.CourseInfoVo;
import com.topera.eduservice.entity.vo.CoursePublish;
import com.topera.eduservice.mapper.EduCourseMapper;
import com.topera.eduservice.service.EduCourseDescriptionService;
import com.topera.eduservice.service.EduCourseService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author puzhenjiang
 * @since 2020-05-18
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {

    @Autowired
    private EduCourseDescriptionService courseDescriptionService;

    @Override
    public String saveCourseInfo(CourseInfoVo courseInfoVo) {

        //1、向课程表添加课程基本信息
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo,eduCourse);
        int count = baseMapper.insert(eduCourse);
        if(count>0){
            EduCourseDescription courseDescription  = new EduCourseDescription();
            courseDescription.setDescription(courseInfoVo.getDescription());
            courseDescription.setId(eduCourse.getId());//两个表的主键一样 一对一关联关系
            //2、向课程简介表中添加课程简介
            courseDescriptionService.save(courseDescription);
        }

        return eduCourse.getId();

    }

    @Override
    public CoursePublish getPublishCourseInfo(String courseId){
        return baseMapper.getPublishCourseInfo(courseId);
    }

    @Override
    public Map<String, Object> getCouseFrontInfo(Page<EduCourse> pageCourse, CourseFrontVo courseFrontVo) {
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(courseFrontVo)){

            //一级分类
            if(!StringUtils.isEmpty(courseFrontVo.getSubjectParentId())){
                wrapper.eq("subject_parent_id",courseFrontVo.getSubjectParentId());
            }
            //二级分类
            if(!StringUtils.isEmpty(courseFrontVo.getSubjectId())){
                wrapper.eq("subject_id",courseFrontVo.getSubjectId());
            }
            //销量buy_count
            if(!StringUtils.isEmpty(courseFrontVo.getBuyCountSort())){
                wrapper.orderByDesc("buy_count");
            }

            //最新发布时间gmt_create
            if(!StringUtils.isEmpty(courseFrontVo.getGmtCreateSort())){
                wrapper.orderByDesc("gmt_create");
            }

            //价格price
            if(StringUtils.isEmpty(courseFrontVo.getPriceSort())){
                wrapper.orderByDesc("price");
            }
        }
        baseMapper.selectPage(pageCourse,wrapper);
        List<EduCourse> records = pageCourse.getRecords();//
        long total = pageCourse.getTotal();//
        long current = pageCourse.getCurrent();//
        long size = pageCourse.getSize();//
        long pages = pageCourse.getPages();//
        boolean hasNext = pageCourse.hasNext();//是否有下一页
        boolean hasPrevious = pageCourse.hasPrevious();//是否有上一页

        //将分页数据获取存入Map集合
        Map<String, Object> map = new HashMap<>();
        map.put("records",records);
        map.put("total",total);
        map.put("size",size);
        map.put("pages",pages);
        map.put("hasNext",hasNext);
        map.put("current",current);
        map.put("hasPrevious",hasPrevious);

        return map;
    }

    @Override
    public CourseWebVo getBaseCourseInfo(String courseId) {
        return baseMapper.getBaseCourseInfo(courseId);
    }
}
